package com.csii.adhoc.controller.dataReady;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.dataready.service.TMgnDirService;
import com.csii.adhoc.dataready.TMgnDirPo;
import com.csii.adhoc.util.DateUtils;
import com.csii.adhoc.util.TreeUtils;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @description: 目录查询、管理
 * @author: zyliu
 * @create: 2021-01-13 19:53
 */
@RestController
@Slf4j
@RequestMapping("/tMgnDir")
public class TMgnDirController {
    @Autowired
    private TMgnDirService tMgnDirService;

    @OperationLog("数据集维护")
    @PostMapping("tMgnDirMaintain")
    public APIResult tMgnDirMaintain(@RequestBody TMgnDirPo vo) {
        String userid = "admin";
        vo.setUpdatedBy(userid)
                .setUpdatedDt(DateUtils.getDateString("yyyyMMddHHmmss"));
        tMgnDirService.tMgnDirMaintain(vo, vo.getOptMode());

        return APIResult.successResult(vo);
    }

    @PostMapping("tMgnDirQuery")
    public APIResult tMgnDirQuery(@RequestBody TMgnDirPo vo) {
        TMgnDirPo po = vo;

        List<TMgnDirPo> list = tMgnDirService.selectListTMgnDir(po);
        //TreeBuilder tb = new TreeBuilder(list);
       // JSONArray ja=tb.buildJSONTree();
//        List<TMgnDirPo> reList = getJsonTMgnDirPo(list,null);
        if (Optional.ofNullable(vo.getTreeFlag()).orElse(true)){
            list = TreeUtils.createTree(list, "menuId",
                    Optional.ofNullable(vo.getMenuId()).orElse("0"));
        }
        return APIResult.successResult(list);
    }

    public static List<TMgnDirPo> getJsonTMgnDirPo(List list,List reList){
        TMgnDirPo pdir = new TMgnDirPo();
        TMgnDirPo sdir = new TMgnDirPo();
        List<TMgnDirPo> sList = new ArrayList<TMgnDirPo>();
        List<TMgnDirPo> pList = new ArrayList<TMgnDirPo>();

        //默认每一个都是父节点
        for(int i = 0;i<list.size();i++){
            pdir = (TMgnDirPo)list.get(i);
            //默认每一个都是子节点
            for(int j = 0; j<list.size();j++){
                sdir = (TMgnDirPo)list.get(j);
                //判断每个子节点的父id等于父节点id并将对象放入list中
                if(pdir.getMenuId().equals(sdir.getParentId())){
                    sList.add(sdir);
                }
            }
            //判断当前父节点是否有子节点
            if(sList != null && sList.size() != 0){
                //如果对象已经包含在Childrens里不再添加进去，以免覆盖
//                boolean b = false;
//                if(pdir.getChildrens()!=null&&pdir.getChildrens().length>0) {
//                    Object[] listTmp = pdir.getChildrens();
//                    for (Object tmdp : listTmp) {
//                        if (((TMgnDirPo)tmdp).getMenuId().equals(sdir.getMenuId())) {
//                            b = true;
//                        }
//                    }
//                }
//                if(!b) {
                    //将获取的子节点list放入数组中
//                    pdir.setChildrens(sList.toArray());
//                }
                //清空list
                sList.clear();
                //将新对象放入list中
                pList.add(pdir);
            }else {
            }
        }
        //判断父节点list是否是空
        if(pList != null && pList.size() != 0){
            //为递归返参做准备
            reList = pList;
            //递归 解决父节点list中还要有父子节点关系
            return getJsonTMgnDirPo(pList, reList);
        }else{
            return reList;
        }

    }

}
